#include "bmp24.h"

int get_bmp_info(char *bmp_path, int *width, int *height)
{

    int fd_bmp = open(bmp_path, O_RDONLY);
    if (fd_bmp < 0)
    {
        printf("open bmp error: %s\n", strerror(errno));
        return -1;
    }
    // printf("open bmp success\n");

    // bmp头
    struct tagBITMAPFILEHEADER file_header;
    struct tagBITMAPINFOHEADER info_header;
    read(fd_bmp, &file_header, sizeof(file_header));
    read(fd_bmp, &info_header, sizeof(info_header));

    // bmp像素信息
    char buf_bmp[480][800 * 3] = {0};
    read(fd_bmp, buf_bmp, sizeof(buf_bmp));

    // printf("file_header.bfType: %x\n", file_header.bfType);
    // printf("file_header.bfSize: %d\n", file_header.bfSize);

    // printf("info_header.biWidth: %d\n", info_header.biWidth);
    // printf("info_header.biHeight: %d\n", info_header.biHeight);
    *width = info_header.biWidth;
    *height = info_header.biHeight;

    close(fd_bmp);
}

int show_bmp_2_lcd(char *bmp_path, int (*lcd_mmap)[800])
{

    int fd_bmp = open(bmp_path, O_RDONLY);
    if (fd_bmp < 0)
    {
        printf("open bmp error: %s\n", strerror(errno));
        return -1;
    }
    // printf("open bmp success\n");

    // bmp头
    struct tagBITMAPFILEHEADER file_header;
    struct tagBITMAPINFOHEADER info_header;
    read(fd_bmp, &file_header, sizeof(file_header));
    read(fd_bmp, &info_header, sizeof(info_header));

    // bmp像素信息
    char buf_bmp[480][800 * 3] = {0};
    read(fd_bmp, buf_bmp, sizeof(buf_bmp));

    // printf("file_header.bfType: %x\n", file_header.bfType);
    // printf("file_header.bfSize: %d\n", file_header.bfSize);

    // printf("info_header.biWidth: %d\n", info_header.biWidth);
    // printf("info_header.biHeight: %d\n", info_header.biHeight);

    // ================

    for (int row = 0; row < 480; row++)
    {
        for (int col = 0; col < 800; col++)
        {

            lcd_mmap[479 - row][col] = buf_bmp[row][col * 3 + 0] << 0 | buf_bmp[row][col * 3 + 1] << 8 | buf_bmp[row][col * 3 + 2] << 16 | 0x00 << 24;
            // 解决上下颠倒 颜色顺序
        }
    }

    close(fd_bmp);
}